/**
 * @description: 欢迎语 选择素材组件类型 配置
 * @return {*}
 */
export const materialTabs = [
    {
        type: '0',
        icon: 'file/pic.png',
        name: '文本',
        field: '',
        id: '1'
    },
    {
        type: '1',
        icon: 'file/pic.png',
        name: '图片',
        field: 'file_name',
        id: '2'
    },
    {
        type: '2',
        icon: 'file/mov.png',
        name: '视频',
        field: 'file_name',
        id: '4'
    },
    {
        type: '3',
        icon: 'file/link.png',
        name: '链接',
        field: 'title',
        id: '6'
    },
    {
        type: '4',
        icon: 'file/mp3.png',
        name: '音频',
        field: 'file_name',
        id: '3'
    },
    {
        type: '5',
        icon: 'file/ysb.png',
        name: '文件',
        field: '',
        id: '5'
    },
    {
        type: '6',
        icon: 'file/wbgg.png',
        name: '抽奖',
        field: 'activity_name',
        id: '8'
    },
    {
        type: '7',
        icon: 'file/shop.png',
        name: '电商',
        field: 'product_name',
        id: '99'
    },
    {
        type: '8',
        icon: 'file/shop.png',
        name: '电商',
        field: 'page_name',
        id: '99'
    },
    {
        type: '9',
        icon: 'file/form.png',
        name: '收集表',
        field: 'title',
        id: '27'
    },
    {
        type: '10',
        icon: 'file/official.png',
        name: '公众号',
        field: '',
        id: '15'
    },
    {
        type: '11',
        icon: 'file/miniApp.png',
        name: '小程序',
        field: 'title',
        id: '11'
    }
];

/**
 * @description: 欢迎语 选择素材组件 映射 map对象
 * @param {*} any
 * @return {*}
 */
export const getHashTabs = () => {
    return materialTabs.reduce((pre, cur) => {
        return { ...pre, [cur.type]: cur };
    }, {});
};

/**
 * @description:
 * @param {any} data
 * @return {*}
 */
export const mapOfficialAccountsData = (data) => {
    const list = [
        { name: '', type: 1, iconUrl: 'file/official.png' },
        { name: '', type: 2, iconUrl: 'file/text.png' },
        { name: '视频', type: 3, iconUrl: 'file/mov.png' },
        { name: '音频', type: 4, iconUrl: 'file/mp3.png' },
        { name: '图片', type: 5, iconUrl: 'file/pic.png' }
    ];
    let { cover: icon } = data;
    const { type: articleType, id, title } = data;
    const targetItem = list.find(el => el.type === articleType);
    const name = targetItem?.name || title;
    const iconUrl = targetItem?.iconUrl;
    const type = '10';
    if (articleType !== 1) {
        icon = iconUrl;
    }
    return { type, name, icon, id, articleType };
};

/**
 * @description: 欢迎语数据回显 初始化  按照统一数据结构模式标准化参数
 * @param {any} data
 * @return {*}
 */
export const initMaterialData = (data) => {
    const hashTabs = getHashTabs();
    return data.map((item) => {
        const {
            type,
            content_id,
            absolute_url,
            title,
            link_title,
            link_url,
            link_decs,
            link_share_image,
            code,
            appid,
            official_article,
            is_default_del: is_default_del = 2
        } = item;
        let { name } = item;
        let officialAccountsData = {};
        const id = content_id;
        const imgUrl = absolute_url;
        let url = '';
        let mpUrl = '';
        const videoUrl = `${absolute_url}?x-oss-process=video/snapshot,t_0,f_jpg,w_320,h_240,m_fast`;
        let linkUrl = '';
        let icon = hashTabs[type.toString()].icon;
        const local = !content_id && !(is_default_del === 1);
        let content = '';
        switch (type) {
            case 1:
                // imgUrl = item.media_file.absolute_url
                name = item.name;
                break;
            case 2:
                url = item.absolute_url;
                // videoUrl = `${absolute_url}?x-oss-process=video/snapshot,t_0,f_jpg,w_320,h_240,m_fast`
                break;
            case 3:
                name = link_title;
                linkUrl = link_share_image;
                content = link_decs;
                break;
            case 4:
                break;
            case 5:
                icon = getSuffixIcon(name);
                break;
            case 6:
                name = link_title;
                content = link_decs;
                break;
            case 7:
                content = link_decs;
            case 8:
                content = link_decs;
            case 9:
                name = link_title;
                content = link_decs;
                break;
            case 10:
                officialAccountsData = mapOfficialAccountsData(official_article);
                break;
            case 11:
                name = title;
                mpUrl = absolute_url;
                content = link_decs;
                break;
        }
        return {
            type: type.toString(),
            code,
            link_title,
            link_url,
            link_decs,
            link_share_image,
            local,
            icon,
            name,
            id,
            url,
            imgUrl,
            mpUrl,
            videoUrl,
            linkUrl,
            appid,
            content,
            is_default_del,
            ...officialAccountsData
        };
    });
};

// 最大图片的尺寸
export const getImageMaxWH = (w, h, W = 1440, H = 1080) => {
    const scale = w / h;
    if (w >= W) {
        w = W;
        h = parseInt(W / scale);
    }
    if (h >= H) {
        h = H;
        w = parseInt(h * scale);
    }
    return { w, h };
};
//获取上传图片的宽高
export const getImage_wh = (file) => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function (e) {
            const img = new Image();
            img.src = e.target.result;
            img.onload = async function () {
                const W = img.width;
                const H = img.height;
                const res = await getImageMaxWH(W, H);
                const w = res.w;
                const h = res.h;
                resolve({ w, h, W, H });
            };
        };
    });
};

// 获取视频、音频的时长
export const playTime = (file, tag = 'video') => {
    return new Promise((resolve, reject) => {
        const ext = file.name.substring(file.name.lastIndexOf('.'));
        // 音频格式。amr  特殊处理
        if (ext === '.amr') {
            const amr = new BenzAMRRecorder();
            amr.initWithBlob(file)
                .then((res) => {
                    const duration = amr.getDuration();
                    resolve(duration);
                })
                .catch((err) => {
                    reject(err);
                });
        } else {
            const reader = new FileReader();
            reader.onload = () => {
                const oVideo = document.createElement(tag);
                oVideo.setAttribute('src', reader.result);
                oVideo.oncanplay = () => {
                    resolve(oVideo.duration);
                };
                oVideo.onerror = e => {
                    reject(e);
                };
            };
            reader.readAsDataURL(file);
        }
    });
};


// 判断是否为空

export const IsNotEmpty = (str) => {
    const type = Object.prototype.toString.call(str);
    switch (type) {
        case '[object String]':
        case '[object Number]':
        case '[object Boolean]':
        case '[object Undefined]':
        case '[object Null]':
            return !!str;
        case '[object Object]':
            return Object.keys(str).length > 0;
        case '[object Array]':
            return str.length > 0;
        default:
            return true;
    }
};


export const validateHttp = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请输入链接地址'));
    } else {
        const reg = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
        if (reg.test(value)) {
            callback();
        } else {
            callback(new Error('请输入带有http或https的正确链接地址'));
        }
    }
};